package com.example.myapplication01.service.tinno;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.myapplication01.R;
import com.example.myapplication01.databinding.ActivityServiceBinding;
import com.example.myapplication01.service.tinno.service.MyService;

public class ServiceActivity extends AppCompatActivity {

    private ActivityServiceBinding binding;
    private MyConn myConn;
    private MyService.MyBinder myBinder;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityServiceBinding.inflate(getLayoutInflater());
        EdgeToEdge.enable(this);
        setContentView(binding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.service), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        /*if (myConn == null) {
            myConn = new MyConn();
        }

        Intent intent = new Intent(this, MyService.class);
        bindService(intent, myConn, BIND_AUTO_CREATE);
        binding.stopServiceBtn.setOnClickListener(v -> unbindService(myConn));
        binding.callBtn.setOnClickListener(v -> myBinder.callServiceTestMethod());*/
    }


    class MyConn implements ServiceConnection {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d("ServiceActivity", "onServiceConnected: onServiceConnected()");
            myBinder = (MyService.MyBinder) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    }



    public void startService() {
        // 开启服务
        Intent intent = new Intent(this, MyService.class);
        startService(intent);
    }

    public boolean stopService(Intent intent) {
        // 关闭服务
        binding.stopServiceBtn.setOnClickListener(v -> {
            Toast.makeText(ServiceActivity.this, "关闭服务", Toast.LENGTH_SHORT).show();
            stopService(intent);
        });
        return false;
    }

}